#! /bin/sh

#Goto here
here="/users/left/guillaume/bots"
cd $here

#BBC Lisbon weather id
id=0048

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=lisbon

#temporary file
file="weather${city}"

#Weather twitter bot
twitbot=login:passw

#Read the RSS feed and filter it
curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" > $file.tmp

#Checks if the message is new or not by counting the number of different lines.
#If the message has unchanged then the number of lines is '0'.
#Otherwise the number of lines is greater than zero.
if [ `diff ${file}.txt ${file}.tmp | wc -l` -gt 0 ]
then

    #Replace the message text
    cp $file.tmp $file.txt

    #Timestamp the log file
    echo ------------------------------------------------------->> $file.log
    date >> $file.log

    #Read the forecast into a weather variable
    read mess < $file.txt
    #echo $mess

    #Twit the weather variable away
    curl --basic --user $twitbot --data status="$mess" http://twitter.com/statuses/update.xml >> $file.log

else

    #Print message
    #echo ${file}.txt and ${file}.log are unchanged

fi

